<template>
  <div class="Container">
    <div class="loginContainer">
      <el-card header="欢迎使用APE文章管理系统">
        <el-form
          :model="formData"
          status-icon
          :rules="rules"
          ref="ruleForm"
          label-width="100px"
          class="demo-ruleForm"
          label-position="left"
        >
          <el-form-item label="账号：" prop="username">
            <el-input
              type="text"
              v-model="formData.username"
              autocomplete="off"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码：" prop="password">
            <el-input
              type="password"
              v-model="formData.password"
              autocomplete="off"
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="submitForm('ruleForm')"
              >提交</el-button
            >

            <el-button type="primary" @click="goToRegister">注册</el-button>
            <el-button type="primary" @click="goTodefault">返回首页</el-button>
          </el-form-item>
        </el-form>
      </el-card>
    </div>
  </div>
</template>

<script>
import Cookies from "js-cookie";
import { login } from "../api/users";
import { setToken } from "../utils/auth";
export default {
  data() {
    return {
      formData: {
        id: 0,
        username: "",
        password: "",
      },
      ruleForm: {
        pass: "",
        checkPass: "",
        age: "",
      },
      rules: {
        username: [
          { required: true, message: "请输入你的账号！", trigger: "blur" },
        ],
        password: [
          { required: true, message: "请输入你的密码！", trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          //保存登录的用户名
          Cookies.set("username", this.formData.username);

          login(this.formData)
            .then(({ data }) => {
              let res = data;
              console.log(res);
              //保存登录用户的ID

              Cookies.set("id", res.data.id);

              if (res.code === 1000) {
                setToken(data.data.token, data.data.refreshToken);
                console.log(data);
                this.$router.push("/");
              } else {
                this.$message.error(res.msg);
              }
            })
            .catch((err) => {
              console.log(err);
              this.$message.error(err);
            });
        } else {
          console.log("error submit!!");
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    goToRegister() {
      this.$router.push("/register");
    },
    goTodefault() {
      this.$router.push("/newIndex");
    },
  },
};
</script>

<style>
body {
  background-color: white;

}
.loginContainer {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 50vh;
  font-weight: 900;
}
.el-card__header {
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 26px;
  font-weight: 900;
}

.el-button {
  margin-top: 5px;
}
.Container {
  width: 1440px;
  height: 900px;
}
</style>